<?php
namespace app\index\controller;

/**
 *  短信验证接口
 */

use think\Db;
use think\Loader;
use think\Request;

class Messageapi extends \think\Controller
{

    public function __construct()
    {
        Loader::import('phpMsgApi\api_sdk\aliyun-php-sdk-core\MsgDemo', EXTEND_PATH);
    }

    //发送短信验证码
    public function sendMsgApi(){

        // 是否为 POST 请求
        if (Request::instance()->isPost()) {
            $postArr = $_POST;


            if($postArr['tel'] == '' || $postArr['tel'] == 'undefined'){
                $ajax['status'] = 0;
                $ajax['msg'] = '手机不能为空';
                return json_encode($ajax);
            }
            if($postArr['oppenId'] == '' || $postArr['oppenId'] == 'undefined'){
                $ajax['status'] = 0;
                $ajax['msg'] = '用户信息获取不全';
                return json_encode($ajax);
            }
            //接收验证手机号码
            $tel = strFilter($postArr['tel']);
            //接收用户唯一标识
            $oppenId = $postArr['oppenId'];

            //判断是否为11位
            if(strlen($tel) == 11){
                $sendMsgApi = new \MsgDemo($tel, $oppenId);
                $verificationId = $sendMsgApi->sendMessage();
                $whereVer = array(
                    'is_delete' => 0,
                    'id' => $verificationId
                );
                //根据返回的id查询验证码结果
                $verificationData = Db::name('wxsp_mobile_verification') -> where($whereVer) -> find();

                if($verificationData['message'] == "OK"){
                    $ajax['status'] = 1;
                    $ajax['requestId'] = $verificationData['requestId'];
                    $ajax['message'] = $verificationData['message'];
                    return json_encode($ajax);
                }else{
                    $ajax['status'] = 2;
                    $ajax['requestId'] = $verificationData['requestId'];
                    $ajax['message'] = $verificationData['message'];
                    return json_encode($ajax);
                }
            }else{
                $ajax['status'] = 0;
                $ajax['msg'] = '手机位数不正确';
                return json_encode($ajax);
            }


        }
    }



    //验证码是否正确
    public function verifyCode(){
        // 使用 Loader 类实例化（单例）
        $order = Loader::model('Order');

        // 是否为 POST 请求
        if (Request::instance()->isPost()) {
            $postArr = $_POST;

//        $postArr['requestId'] = '820772B3-01D9-4C39-AA13-7CA3A517F7AA';
//        $postArr['code'] = '281988';

            if($postArr['requestId'] == '' || $postArr['code'] == ''){
                $ajax['status'] = 0;
                $ajax['msg'] = '验证码不能为空';
                return json_encode($ajax);
            }

            $code = strFilter($postArr['code']);
            $requestId = $postArr['requestId'];
            //查询该条验证信息
            $whereVer = array(
                'is_delete' => 0,
                'requestId' => $requestId,
                'message' => 'ok'
            );

            $verificationData = Db::name('wxsp_mobile_verification') -> where($whereVer) -> find();

            //验证
            if($code == $verificationData['verification_code']){
                //更新本表状态
                $updateVer = array(
                    'status' => 1
                );
                $verRes = Db::name('wxsp_mobile_verification') -> where($whereVer) -> update($updateVer);


                    //判断是否符合奖励规则
                    $isUserTel = Db::name('wxuser') -> where('openId',$verificationData['openId']) -> field('tel') -> find();

                    if($isUserTel['tel'] == ''){

                        //系统奖励-100积分
                        $orderOpenId = $verificationData['openId'];
                        $orderName = '系统奖励100积分';
                        $orderValue = 100;
                        $orderMoney = 0;
                        $orderTime = time();
                        $orderSource = '系统充值';
                        $orderStatus = 0;
                        $orderNote = '用户通过手机验证通过，系统奖励100积分';
                        $order -> postOrder($orderOpenId,$orderName,$orderValue,$orderMoney,$orderTime,$orderSource,$orderStatus,$orderNote);
                    }

                    //更新用户表手机
                    $whereUser = array(
                        'openId' => $verificationData['openId']
                    );
                    $updateUser = array(
                        'tel' => $verificationData['tel']
                    );
                    $resUser = Db::name('wxuser') -> where($whereUser) -> update($updateUser);
                    if($resUser){

                        $ajax['status'] = 1;
                        $ajax['msg'] = '验证成功';
                        return json_encode($ajax);
                    }

            }else{
                $ajax['status'] = 0;
                $ajax['msg'] = '验证码错误';
                return json_encode($ajax);
            }

        }
    }
}